<?php
namespace Job\Controller;
use Common\Controller\Base;
class IndexController extends Base {
    //招聘模型对象
    private $Job = NULL;
    //应聘模型对象
    private $Candidates = NULL;

    protected function _initialize() {
        parent::_initialize();
        $this->Job = D('Job/Job');
        $this->Candidates = D('Job/Candidates');
    }

    //招聘首页
    public function index() {
        //条件
        $where = array();
        $keyword = I('get.keyword');
        if ($keyword) {
            $where['zhiwei'] = array('LIKE', "%{$keyword}%");
            $this->assign('keyword', $keyword);
        }

        $count = $this->Job->where($where)->count();
        $page = $this->page($count, 20);
        $data = $this->Job->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("listorder" => "DESC", "jobid" => "DESC"))->select();

        $this->assign('list', $data);
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->display();
    }

    //职位内页
    public function info() {
        $jobId = I('get.jobid', 0, 'intval');
        if (empty($jobId)) {
            $this->error('该招聘职位不存在！');
        }
        $info = $this->Job->getInfo($jobId);
        if (empty($info)) {
            $this->error('该招聘职位不存在！');
        }
        $seo = seo('', $info['zhiwei'], strip_tags($info['content']), $info['zhiwei']);

        $this->assign('SEO', $seo);
        $this->assign('info', $info);
        $this->display();
    }

    //应聘
    public function yingpin() {
        if (IS_POST) {
            $post = I('post.', NULL, 'htmlspecialchars');
            $post['username'] = service("Passport")->usename ? : "";
            $post['userid'] = service("Passport")->userid ? : 0;
            if ($this->Candidates->addCandidates($post)) {
                $this->success('提交成功！', U('Index/index'));
            } else {
                $error = $this->Candidates->getError();
                $this->error($error ? $error : '提交失败！');
            }
        } else {
            $jobId = I('get.jobid', 0, 'intval');
            if (empty($jobId)) {
                $this->error('该招聘职位不存在！');
            }
            $info = $this->Job->getInfo($jobId);
            if (empty($info)) {
                $this->error('该招聘职位不存在！');
            }
            //截止时间
            if ($info['enddate'] <= time()) {
                $this->error('该职位招聘已经结束！');
            }
            $seo = seo('', "应聘" . $info['zhiwei'], strip_tags($info['content']), $info['zhiwei']);

            $this->assign('SEO', $seo);
            $this->assign('info', $info);
            $this->assign('jobid', $jobId);
            $this->display();
        }
    }

}
